package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AttendanceRecords;
import com.ruoyi.system.service.IAttendanceRecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考勤记录Controller
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/system/records")
public class AttendanceRecordsController extends BaseController
{
    @Autowired
    private IAttendanceRecordsService attendanceRecordsService;

    /**
     * 查询考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:records:list')")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceRecords attendanceRecords)
    {
        startPage();
        List<AttendanceRecords> list = attendanceRecordsService.selectAttendanceRecordsList(attendanceRecords);
        return getDataTable(list);
    }

    /**
     * 导出考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:records:export')")
    @Log(title = "考勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceRecords attendanceRecords)
    {
        List<AttendanceRecords> list = attendanceRecordsService.selectAttendanceRecordsList(attendanceRecords);
        ExcelUtil<AttendanceRecords> util = new ExcelUtil<AttendanceRecords>(AttendanceRecords.class);
        util.exportExcel(response, list, "考勤记录数据");
    }

    /**
     * 获取考勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:records:query')")
    @PostMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        return success(attendanceRecordsService.selectAttendanceRecordsById(id));
    }

    /**
     * 新增考勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:records:add')")
    @Log(title = "考勤记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceRecords attendanceRecords)
    {
        return toAjax(attendanceRecordsService.insertAttendanceRecords(attendanceRecords));
    }

    /**
     * 修改考勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:records:edit')")
    @Log(title = "考勤记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceRecords attendanceRecords)
    {
        return toAjax(attendanceRecordsService.updateAttendanceRecords(attendanceRecords));
    }

    /**
     * 删除考勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:records:remove')")
    @Log(title = "考勤记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attendanceRecordsService.deleteAttendanceRecordsByIds(ids));
    }


    @GetMapping("/todayCheckInStats")
    public AjaxResult getTodayCheckInStats()
    {
        List<Map<String, Object>> data = attendanceRecordsService.getTodayCheckInStats();
        System.out.println("今日签到数据为：" + data);
        System.out.println();
        return AjaxResult.success(data);
    }

    @PostMapping("/getMonthRecordsById/{id}")
    public AjaxResult getMonthRecordsById(@PathVariable("id") Long id)
    {
        List<AttendanceRecords> records = attendanceRecordsService.getMonthRecordsById(id);
        return AjaxResult.success(records); // 确保返回多条记录
    }
    /**
     * 导入考勤记录数据
     */
    @PreAuthorize("@ss.hasPermi('system:records:import')")
    @Log(title = "考勤记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) {
        try {
            attendanceRecordsService.importRecords(file);
            return AjaxResult.success("导入成功");
        } catch (Exception e) {
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }
}
